package com.xsy.android.compose.we.component.ui.page.chat

import android.util.Log
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.spring
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.xsy.android.compose.we.component.R
import com.xsy.android.compose.we.component.ui.compose.WeTopBar
import com.xsy.android.compose.we.component.ui.page.chat.compose.ChatBottomBar
import com.xsy.android.compose.we.component.ui.page.chat.compose.MessageItem
import com.xsy.android.compose.we.component.ui.theme.WeComposeTheme
import com.xsy.android.compose.we.component.view_model.WeViewModel

/**
 *@author: xiaosy
 *时间: 2024/7/1
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@Composable
fun ChatPage(modifier: Modifier = Modifier, navController: NavHostController, index: Int) {
    val viewModel: WeViewModel = viewModel()
    val chat = viewModel.chats[index]
    Log.e("ChatPage", "viewModel=$viewModel, chat=$chat")
    Scaffold(modifier = modifier) { innerPadding ->
        Column(
            modifier = modifier
                .padding(innerPadding)
                .background(WeComposeTheme.colors.background)
                .fillMaxSize()
        ) {
            WeTopBar(title = chat.friend.name) {
                navController.popBackStack()
            }
            var shakingTime by remember {
                mutableIntStateOf(0)
            }
            Box(
                modifier = Modifier
                    .background(WeComposeTheme.colors.chatPage)
                    .weight(1f)
            ) {
                //新年版本，无数据的情况下显示界面
                Box(
                    modifier = Modifier
                        .alpha(WeComposeTheme.colors.chatPageBgAlpha)
                        .fillMaxSize()
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.ic_bg_newyear_left),
                        contentDescription = null,
                        modifier = Modifier
                            .align(Alignment.CenterStart)
                            .padding(bottom = 100.dp)
                    )
                    Image(
                        painter = painterResource(id = R.drawable.ic_bg_newyear_top),
                        contentDescription = null,
                        modifier = Modifier
                            .align(Alignment.TopEnd)
                            .padding(bottom = 24.dp)
                    )
                    Image(
                        painter = painterResource(id = R.drawable.ic_bg_newyear_right),
                        contentDescription = null,
                        modifier = Modifier
                            .align(Alignment.BottomEnd)
                            .padding(bottom = 200.dp)
                    )
                }
                val shakingOffset = remember {
                    Animatable(0f)
                }
                LaunchedEffect(key1 = shakingTime) {
                    if (shakingTime != 0) {
                        shakingOffset.animateTo(
                            0f,
                            animationSpec = spring(0.3f, 600f),
                            initialVelocity = -2000f
                        )
                    }
                }
                LazyColumn(
                    modifier = Modifier
                        .fillMaxSize()
                        .offset(shakingOffset.value.dp, shakingOffset.value.dp)
                ) {
                    items(chat.msgs.size) { index ->
                        val msg = chat.msgs[index]
                        MessageItem(msg, shakingTime, chat.msgs.size - index - 1)
                    }
                }
            }
            ChatBottomBar(onBombClicked = {
                viewModel.boom(chat)
                shakingTime++
            })
        }
    }

}


@Preview(showBackground = true)
@Composable
fun PreviewChatPage() {
    val navController = rememberNavController()
    ChatPage(navController = navController, index = 0)
}


